//
//  mainMenuScreen.m
//  whack
//
//  Created by aneuman1 on 11/7/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "mainMenuScreen.h"
#import <AudioToolbox/AudioToolbox.h>

@implementation mainMenuScreen
@synthesize muteswitch;

//chocomoles
@synthesize bananaMole;
@synthesize bluMole;
@synthesize chocoMole;
@synthesize strawMole;

@synthesize hole1;
@synthesize hole2;
@synthesize hole3;
@synthesize hole4;
@synthesize hole5;

@synthesize mole1;
@synthesize mole2;
@synthesize mole3;
@synthesize mole4;
@synthesize mole5;

//@synthesize labeltest;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [labeltest release];
    [muteswitch release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


- (void)viewDidLoad
{
    NSString *path=[[NSBundle mainBundle] pathForResource:@"Wallpaper" ofType:@"mp3"];
    theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate = self;
    theAudio.numberOfLoops = -1;
    [theAudio play];
    //[theAudio setVolume:0];
       
    [self moveUp:nil finished:nil context:nil];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setLabeltest:nil];
    [self setMuteswitch:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

-(IBAction)readPlayButtonPressed {
    //Allocate a TextViewController and initialize it from a nib.
    //This line should look familiar
    whackViewController *textVC = 
    [[[whackViewController alloc] initWithNibName:@"whackViewController"
                                           bundle:nil]autorelease];
    
    //Finally, show our new view. The view controller will
    //load our file as the view appears
    [self presentModalViewController:textVC animated:YES];
}

-(IBAction)readHighScoreButtonPressed {
    //Allocate a TextViewController and initialize it from a nib.
    //This line should look familiar
    highScoreScreen *textVC = 
    [[[highScoreScreen alloc] initWithNibName:@"highScoreScreen"
                                          bundle:nil]autorelease];

    //Finally, show our new view. The view controller will
    //load our file as the view appears
    [self presentModalViewController:textVC animated:YES];
}


//moving up and down
- (void)moveUp:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.25];
    [UIView setAnimationDelay:0.7];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(moveDown:finished:context:)];
    bananaMole.center = CGPointMake(102, 117);
    [UIView setAnimationDuration:0.5];
    bluMole.center = CGPointMake(198, 117);
    [UIView setAnimationDuration:0.75];
    chocoMole.center = CGPointMake(291, 117);
    [UIView setAnimationDuration:1.0];
    strawMole.center = CGPointMake(379, 117);
    [UIView commitAnimations];
    
}

- (void)moveDown:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.25];
    [UIView setAnimationDelay:0.7];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDidStopSelector:@selector(moveUp:finished:context:)];
    bananaMole.center = CGPointMake(102, 150);
    [UIView setAnimationDuration:.5];
    bluMole.center = CGPointMake(198, 150);
    [UIView setAnimationDuration:.75];
    chocoMole.center = CGPointMake(291, 150);
    [UIView setAnimationDuration:1.0];
    strawMole.center = CGPointMake(379, 150);
    [UIView commitAnimations];
    
}


- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];
    
    //sound
    NSString *squishPath = [[NSBundle mainBundle] 
                            pathForResource:@"smack" ofType:@"wav"];
    NSURL *squishURL = [NSURL fileURLWithPath:squishPath];
    SystemSoundID squishSoundID;
    AudioServicesCreateSystemSoundID((CFURLRef)squishURL, &squishSoundID);
    
    CGRect hole1Rect = [[[hole1 layer] presentationLayer] frame];
    if(hole1active)
    {
        if(CGRectContainsPoint(hole1Rect, touchLocation))
        {
            
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:.5];
        [UIView setAnimationDelay:0.0];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDelegate:self];
        mole1.center = CGPointMake(58, 71);
        [UIView commitAnimations];            
            [UIView animateWithDuration:0.1 
                                  delay:0.0 
                                options:UIViewAnimationCurveEaseOut
                             animations:^{                              
                                 mole1.transform = CGAffineTransformMakeScale(1.25, 0.75);
                             } 
                             completion:^(BOOL finished) {  
                                 [UIView animateWithDuration:.1 
                                                       delay:0.0 
                                                     options:0
                                                  animations:^{                      
                                                      mole1.alpha = 0.0;
                                                  } completion:^(BOOL finished) {
                                                      [mole1 removeFromSuperview];
                                                      mole1 = nil;
                                                  }];                 
                             }];

            if(!moled1)
            {

            AudioServicesPlaySystemSound(squishSoundID);
                moled1 = YES;
            }
            
            
            

        //hole1active = false;
            
            //labeltest.text = @"test";
            
            return;
        
        }

    }
    else if(!hole1active)
    {

        
    
    if(CGRectContainsPoint(hole1Rect, touchLocation))
    {
        NSLog(@"hole1 active");
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:.5];
        [UIView setAnimationDelay:0.0];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDelegate:self];
        mole1.center = CGPointMake(58, 30);
        [UIView commitAnimations];
        hole1active = true;
    }
    }
    
    CGRect hole2Rect = [[[hole2 layer] presentationLayer] frame];
    if(hole2active)
    {
        
        if(CGRectContainsPoint(hole2Rect, touchLocation))
        {
            /*[UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:.5];
            [UIView setAnimationDelay:0.0];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            [UIView setAnimationDelegate:self];
            mole2.center = CGPointMake(148, 71);
            [UIView commitAnimations];
            hole2active = false;*/
            
            [UIView animateWithDuration:0.1 
                                  delay:0.0 
                                options:UIViewAnimationCurveEaseOut
                             animations:^{                              
                                 mole2.transform = CGAffineTransformMakeScale(1.25, 0.75);
                             } 
                             completion:^(BOOL finished) {  
                                 [UIView animateWithDuration:.1 
                                                       delay:.0 
                                                     options:0
                                                  animations:^{                      
                                                      mole2.alpha = 0.0;
                                                  } completion:^(BOOL finished) {
                                                      [mole2 removeFromSuperview];
                                                      mole2 = nil;
                                                  }];                 
                             }];
            
            if(!moled2)
            {
            AudioServicesPlaySystemSound(squishSoundID);
                moled2 = YES;
            }
            return;
        }
        
    }
    else if(!hole2active)
    {
        
        
        
        if(CGRectContainsPoint(hole2Rect, touchLocation))
        {
            NSLog(@"hole2 active");
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:.5];
            [UIView setAnimationDelay:0.0];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            [UIView setAnimationDelegate:self];
            mole2.center = CGPointMake(148, 30);
            [UIView commitAnimations];
            hole2active = true;
        }
    }
    
    CGRect hole3Rect = [[[hole3 layer] presentationLayer] frame];
    if(hole3active)
    {
        if(CGRectContainsPoint(hole3Rect, touchLocation))
        {
            /*[UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:.5];
            [UIView setAnimationDelay:0.0];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            [UIView setAnimationDelegate:self];
            mole3.center = CGPointMake(240, 71);
            [UIView commitAnimations];
            hole3active = false;*/
            
            [UIView animateWithDuration:0.1 
                                  delay:0.0 
                                options:UIViewAnimationCurveEaseOut
                             animations:^{                              
                                 mole3.transform = CGAffineTransformMakeScale(1.25, 0.75);
                             } 
                             completion:^(BOOL finished) {  
                                 [UIView animateWithDuration:.1 
                                                       delay:.0 
                                                     options:0
                                                  animations:^{                      
                                                      mole3.alpha = 0.0;
                                                  } completion:^(BOOL finished) {
                                                      [mole3 removeFromSuperview];
                                                      mole3 = nil;
                                                  }];                 
                             }];
            
            if(!moled3)
            {
            AudioServicesPlaySystemSound(squishSoundID);
                moled3 = YES;
            }
            return;
        }
        
    }
    else if(!hole3active)
    {
        
        
        
        if(CGRectContainsPoint(hole3Rect, touchLocation))
        {
            NSLog(@"hole3 active");
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:.5];
            [UIView setAnimationDelay:0.0];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            [UIView setAnimationDelegate:self];
            mole3.center = CGPointMake(240, 30);
            [UIView commitAnimations];
            hole3active = true;
        }
    }
    
    CGRect hole4Rect = [[[hole4 layer] presentationLayer] frame];
    if(hole4active)
    {
        if(CGRectContainsPoint(hole4Rect, touchLocation))
        {
            /*[UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:.5];
            [UIView setAnimationDelay:0.0];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            [UIView setAnimationDelegate:self];
            mole4.center = CGPointMake(332, 71);
            [UIView commitAnimations];
            hole4active = false;*/
            
            [UIView animateWithDuration:0.1 
                                  delay:0.0 
                                options:UIViewAnimationCurveEaseOut
                             animations:^{                              
                                 mole4.transform = CGAffineTransformMakeScale(1.25, 0.75);
                             } 
                             completion:^(BOOL finished) {  
                                 [UIView animateWithDuration:.1 
                                                       delay:.0 
                                                     options:0
                                                  animations:^{                      
                                                      mole4.alpha = 0.0;
                                                  } completion:^(BOOL finished) {
                                                      [mole4 removeFromSuperview];
                                                      mole4 = nil;
                                                  }];                 
                             }];
            
            if(!moled4)
            {
            AudioServicesPlaySystemSound(squishSoundID);
                moled4=YES;
            }
            return;
        }
        
    }
    else if(!hole4active)
    {
        
        
        
        if(CGRectContainsPoint(hole4Rect, touchLocation))
        {
            NSLog(@"hole4 active");
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:.5];
            [UIView setAnimationDelay:0.0];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            [UIView setAnimationDelegate:self];
            mole4.center = CGPointMake(332, 30);
            [UIView commitAnimations];
            hole4active = true;
        }
    }
    
    CGRect hole5Rect = [[[hole5 layer] presentationLayer] frame];
    if(hole5active)
    {
        if(CGRectContainsPoint(hole5Rect, touchLocation))
        {
            /*[UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:.5];
            [UIView setAnimationDelay:0.0];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            [UIView setAnimationDelegate:self];
            mole5.center = CGPointMake(422, 71);
            [UIView commitAnimations];
            hole5active = false;*/
            
            [UIView animateWithDuration:0.1 
                                  delay:0.0 
                                options:UIViewAnimationCurveEaseOut
                             animations:^{                              
                                 mole5.transform = CGAffineTransformMakeScale(1.25, 0.75);
                             } 
                             completion:^(BOOL finished) {  
                                 [UIView animateWithDuration:.1 
                                                       delay:.0 
                                                     options:0
                                                  animations:^{                      
                                                      mole5.alpha = 0.0;
                                                  } completion:^(BOOL finished) {
                                                      [mole5 removeFromSuperview];
                                                      mole5 = nil;
                                                  }];                 
                             }];
            
            if(!moled5)
            {
            
            AudioServicesPlaySystemSound(squishSoundID);
                moled5=YES;
            }
            return;
        }
        
    }
    else if(!hole5active)
    {
        
        
        
        if(CGRectContainsPoint(hole5Rect, touchLocation))
        {
            NSLog(@"hole5 active");
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:.5];
            [UIView setAnimationDelay:0.0];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            [UIView setAnimationDelegate:self];
            mole5.center = CGPointMake(422, 30);
            [UIView commitAnimations];
            hole5active = true;
        }
    }
    

    
    

}

- (IBAction) switchValueChanged{
    
    if (muteswitch.on) {
        /*NSString *path=[[NSBundle mainBundle] pathForResource:@"Wallpaper" ofType:@"mp3"];
        theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
        theAudio.delegate = self;
        theAudio.numberOfLoops = -1;
        [theAudio play];*/
        [theAudio setVolume:1.0];
        //[theAudio play];
    }
    else if(!muteswitch.on)
    { [theAudio setVolume:0.0]; }
}

-(IBAction) toggleButtonPressed{
    	  if(muteswitch.on){
        	    [muteswitch setOn:NO animated:YES];
        	  }
    	  else{
        	    [muteswitch setOn:YES animated:YES];
        	 
        	  }
    	 
    	}



@end
